package com.example.wyemail;

import com.example.wyemail.util.DataUtil;
import com.example.wyemail.util.IconUtil;
import com.example.wyemail.util.SoftInitUtil;
import org.apache.ibatis.annotations.Mapper;
import org.sikuli.script.*;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;

import static com.example.wyemail.util.EmailAccountUtil.addEmailAccount;

@Component
public class MainEmail {
    public void AutoStart(String softDirectory,String accountDocument,String mainDirectory,String exportDirectory) throws InterruptedException, IOException, IconUtil.ImageNotFoundException, FindFailed {
        //打开客户端
        SoftInitUtil.Start(softDirectory);
        Thread.sleep(5000);
        //打开添加邮箱页面
        File directory = new File("");
        String courseFile = directory.getCanonicalPath() ;
        String imgPath = courseFile + "\\src\\main\\resources\\images\\";
        Screen s = new Screen();
        String icon1Img = imgPath + "icon1.png";
        String emailSetImg = imgPath + "emailSet.png";
        Thread.sleep(3000);
        if(IconUtil.IsExist(s,icon1Img)){
            Pattern icon1 = new Pattern(imgPath + "icon1.png");
            Match match = s.find(icon1);
            Location topLeft = match.getTopLeft();
            int topLeftX = topLeft.getX();
            int topLeftY = topLeft.getY();
            int targetX = topLeftX + 28;
            int targetY = topLeftY + 276;
            s.click(new Location(targetX, targetY));
        }
        if(IconUtil.IsExist(s,emailSetImg)){
            s.click(emailSetImg);
        }
        Thread.sleep(1000);
        List<String[]> emailAccounts = new ArrayList<>();

        // 读取文件并解析内容
        try (BufferedReader br = new BufferedReader(new FileReader(accountDocument))) {
            String line;
            // 从第1行开始读取
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\\s+");
                if (parts.length >= 2) {
                    String email = parts[0];
                    String password = parts[1];
                    String phone = parts[2];
                    emailAccounts.add(new String[]{email, password, phone});
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 循环添加每个邮箱账号
        Integer emailSum = 0;
        for (String[] account : emailAccounts) {
            String email = account[0];
            String password = account[1];
            addEmailAccount(email, password);
            //删除邮箱，默认是保留本地数据
//            if(emailSum!=0){
//                String deleteEmailImg = imgPath + "deleteEmail.png";
//                //不保留的图标路径
//                String noSaveDataImg = imgPath + "noSaveData.png";
//                //保留的图标路径
//                String saveDataImg = imgPath + "saveData.png";
//                if(IconUtil.IsExist(s,deleteEmailImg)){
//                    s.click(deleteEmailImg);
//                    Thread.sleep(300);
//                    if(IconUtil.IsExist(s,noSaveDataImg)){
//                        s.click(noSaveDataImg);
//                    }
//                }
//            }
            emailSum++;
        }

        DataUtil.getData(mainDirectory,accountDocument,exportDirectory);

        System.out.println("终止程序");
        System.exit(0);
        System.out.println("已关闭客户端");

    }
}
